<?php

class product_model extends CI_Model {
    public function __construct() {
        parent::__construct();
    }
    
    public function getProductByCategoryId($cat_id){
        $query = $this->db->query("select PRD_ID
                                    , PRD_NAME
                                    , SALE_PRICE
                                    , PRD_IMG_1  
                                  from GS_PRODUCT 
                                  where IS_ACTIVE = 'Y' 
                                  and CAT_ID = ".$cat_id); 
        
        return $query;
    }
    
    public function getProductByCategoryIdAndBrandId($cat_id, $brand_id){
        $query = $this->db->query("select PRD_ID
                                    , PRD_NAME
                                    , SALE_PRICE
                                    , PRD_IMG_1  
                                  from GS_PRODUCT 
                                  where IS_ACTIVE = 'Y' 
                                  and CAT_ID = ".$cat_id." 
                                  and BRAND_ID = ".$brand_id
                ); 
        
        return $query;
    }
    
    public function getGroupOfBrandByCatId($cat_id){
        $query = $this->db->query("
        
            select count(*) as NUMBER, br.BRAND_NAME, bl.BRAND_ID
            from (
                    select pd.BRAND_ID
                    from gs_product pd
                    where pd.CAT_ID = ".$cat_id."
            ) as bl
            inner join gs_brand br on bl.brand_id = br.brand_id
            group by bl.BRAND_ID
            having bl.BRAND_ID is not null
        
        ");
        
        return $query;
    }
    
    public function getProductDetailById($prd_id){
        $query = $this->db->query('
            select * from gs_product 
            where prd_id = '.$prd_id.'
        ');
        
        return $query;
    }
    
    public function getSalePriceById($prd_id){
        $query = $this->db->query('select SALE_PRICE from gs_product where prd_id = '.$prd_id);
        $row = $query->row(); 
        
        if($row){
            return $row->SALE_PRICE;
        } else {
            log_message('error', get_class($this)." // Can't find Product Id :: ".$prd_id);
        }
    }
    
    

}
?>
